//
//  ViewController.m
//  Core Bluetooth
//
//  Created by 董文博 on 15/2/11.
//
//


#import "ViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>

@interface ViewController () <CBCentralManagerDelegate, CBPeripheralDelegate>
@property (nonatomic, strong) CBCentralManager *mgr;
@property (nonatomic, strong) NSMutableArray *peripherals;

@property (nonatomic, strong) CBCharacteristic *dataInteractCharacteristic;
@property (nonatomic, strong) CBCharacteristic *peripheralInfoCharacteristic;
@end

@implementation ViewController

- (NSMutableArray *)peripherals
{
    if (!_peripherals) {
        self.peripherals = [NSMutableArray array];
    }
    return _peripherals;
}

- (CBCentralManager *)mgr
{
    if (!_mgr) {
        // 创建中心设备管理者，用来管理中心设备
        self.mgr = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
    }
    return _mgr;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 扫描外设
#warning 通过传入一个存放服务UDID的数组进去，过滤掉一些不要的外设
    [self.mgr scanForPeripheralsWithServices:@[@"434", @"435435"] options:nil];
}

/**
 *  点击按钮，建立连接
 */
- (void)buildConnect
{
    for (CBPeripheral *peripheral in self.peripherals) {
        [self.mgr connectPeripheral:peripheral options:nil];
    }
}

#pragma mark - CBCentralManagerDelegate
/**
 *  发现外围设备的时候调用
 */
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    // 添加外围设备
    if (![self.peripherals containsObject:peripheral]) {
        // 设置外设的代理
        peripheral.delegate = self;
        [self.peripherals addObject:peripheral];
    }
}

/**
 *  连接到某个外设的时候调用
 */
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
    // 查找外设中的所有服务
#warning 通过传入一个存放服务UDID的数组进去，过滤掉一些不要的服务
    [peripheral discoverServices:@[@"434", @"435435"]];
}

/**
 *  跟某个外设失去连接
 */
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
    
}

#pragma mark - CBPeripheralDelegate
/**
 *  外设已经查找到服务
 */
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
    // 遍历所有的服务
    for (CBService *service in peripheral.services) {
        // 过滤掉不想要的服务
        if ([service.UUID isEqual:@"123"]) {
            // 找到想要的服务
            
            // 扫描服务下面的特征
#warning 通过传入一个存放特征UDID的数组进去，过滤掉一些不要的特征
            [peripheral discoverCharacteristics:@[@"435", @"6456"] forService:service];
        }
    }
}

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
    // 遍历所有的特征
    for (CBCharacteristic *characteristic in service.characteristics) {
        // 过滤掉不想要的特征
        if ([characteristic.UUID isEqual:@"456"]) {
            // 找到想要的特征
            self.dataInteractCharacteristic = characteristic;
        } else if ([characteristic.UUID isEqual:@"789"]) {
            self.peripheralInfoCharacteristic = characteristic;
        }
    }
}
@end
